package com.jrx.anytxn.transaction.job.marketingFile.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.MarketingFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Service
public class MergeMarketingFileListener implements JobExecutionListener {
    private static final Logger logger = LoggerFactory.getLogger(MergeMarketingFileListener.class);

    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private MarketingFileConfig marketingFileConfig;

    @Override
    public void beforeJob(JobExecution jobExecution) {
        String basePath = marketingFileConfig.getMarketingFilePath();
        String outPath = marketingFileConfig.getOutDir();

        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        String toDay = DateUtils.format(org.getToday(),"yyyyMMdd");

        String marketSumFileName = org.getTenantId() + "_" + TransBizConstant.FILE_NAME_MARKET_SUM + "_" + "ADD" + "_" + toDay + ".del";
        String marketDetFileName = org.getTenantId() + "_" + TransBizConstant.FILE_NAME_MARKET_DET + "_" + "ADD" + "_" + toDay + ".del";
        String marketSignFileName = "indicate.txt";
        String tarFileName = toDay + "_" + org.getTenantId() + ".tar"; //输出压缩包
        String okFileName = toDay + "_" + org.getTenantId() + ".ok";   //ok文件

        String reportingPath = basePath + org.getTenantId() + File.separator + toDay + File.separator;
        Path outFilePath = Paths.get(reportingPath + outPath);    //输出压缩包所在目录

        List<Path> reportingFileList = new ArrayList<>();
        reportingFileList.add(Paths.get(reportingPath + marketSumFileName));
        reportingFileList.add(Paths.get(reportingPath + marketDetFileName));
        reportingFileList.add(Paths.get(reportingPath + marketSignFileName));
        reportingFileList.add(Paths.get(reportingPath + outPath + tarFileName));
        reportingFileList.add(Paths.get(reportingPath + outPath + okFileName));

        //判断文件夹是否存在
        if (!Files.isDirectory(outFilePath)) {
            //创建文件夹
            try {
                Files.createDirectories(outFilePath);
            } catch (IOException e) {
                logger.error("创建目录：{},失败：{}", outFilePath, e);
            }
        }

        // 文件已存在，先删除
        for (Path reportingFile : reportingFileList) {
            if (Files.exists(reportingFile)) {
                try {
                    Files.delete(reportingFile);
                } catch (IOException e) {
                    logger.error("删除文件：{}，失败：{}", reportingFile, e);
                }
            }
        }
    }

    @Override
    public void afterJob(JobExecution jobExecution) {

    }
}
